#!/usr/bin/env bash

DEFAULT_HOST="https://gitee.com/tj123/common/raw/master"
HOST="http://<%= $host %>"
if [[ "$HOST" =~ ^http:\/\/\<.*\>$ ]];then
  HOST=$DEFAULT_HOST
fi

ORACLE_HOME="/home/oracle/app/oracle/product/11.2.0/dbhome_1"
ORACLE_SID="orcl"
USER="oracle"
PASSWORD="123456"

for pm in "$@"; do
  case "$pm" in
    --ORACLE_HOME=*)
      ORACLE_HOME="${pm#*=}";;
    --ORACLE_SID=*)
      ORACLE_SID="${pm#*=}";;
    --USER=*)
      USER="${pm#*=}";;
    --PASSWORD=*)
      PASSWORD="${pm#*=}";;
    *)
    ;;
  esac
done

# 备份文件
backup(){
  file="$1"
  ls -a $file.*.bak >/dev/null 2>&1 || mv "$file" "$file.$(date +%s).bak"
}

# 替换配置内容
replace_config(){
  file="$1"
  content="$(cat "$file")"
  shift
  for conf in "$@"; do
    # 获取键名
    key="$(echo "$conf"|grep -o '^[^=]*'|sed 's/^\s*//;s/\s*$//')"
    # 文件里的内容
    f_cont="$(echo "$content"|grep "^\s*$key\s*")"
    if [ -n "$f_cont" ]; then
      # 文件里面的内容是否配置一样
      if [ "$(echo "$f_cont"|sed 's/^\s*#\\?\s*//;s/\s*$//')" != "$conf" ]; then
        content="$(echo "$content"|sed -e "s/^\s*$key\s*=/# &/g" -e "/^#\s*$key\s*=/a$conf")"
      fi
    else
      # 文件里面有被注释了的在下面添加
      if echo "$content"|grep -q "^\s*#\?\s*$key\s*="; then
        content="$(echo "$content"|sed -e "/^\s*#\?\s*$key\s*=/a$conf")"
      else
        content="$(echo -e "$content\n$conf")"
      fi
    fi
  done
  backup "$file"
  echo "$content" > "$file"
}

SYSCTL_CONF=("fs.aio-max-nr = 1048576"
"fs.file-max = 6815744"
"kernel.shmall = 2097152"
"kernel.shmmax = 536870912"
"kernel.shmmni = 4096"
"kernel.sem = 250 32000 100 128"
"net.ipv4.ip_local_port_range = 9000 65500"
"net.core.rmem_default = 262144"
"net.core.rmem_max = 4194304"
"net.core.wmem_default = 262144"
"net.core.wmem_max = 1048586")

LIMITS_CONF=("oracle soft nproc 2047"
"oracle hard nproc 16384"
"oracle soft nofile 1024"
"oracle hard nofile 65536")

LOGIN_CONF=("session required pam_limits.so")

err_quit(){ if [ $1 -ne 0 ]; then echo $2; exit 1; fi;}

# 开始安装的第一步
step1(){
  yum -y install binutils compat-libcap1 compat-libstdc++.i686 compat-libstdc++.x86_64 elfutils-libelf \
    elfutils-libelf-devel gcc gcc-c++ glibc.i686 glibc.x86_64 glibc-devel.i686 glibc-devel.x86_64 ksh libgcc.i686 \
    libgcc.x86_64 libstdc++.i686 libstdc++.x86_64 libstdc++-devel.i686 libstdc++-devel.x86_64 libaio.i686 \
    libaio.x86_64 libaio-devel.i686 libaio-devel.x86_64 libXext libXtst libX11 libXau libxcb libXi make \
    sysstat unixODBC-devel.i686 unixODBC-devel.x86_64 unixODBC.i686 unixODBC.x86_64 compat-libstdc++-33.i686 compat-libstdc++-33.x86_64
  replace_config /etc/sysctl.conf "${SYSCTL_CONF[@]}"
  sysctl -p
  replace_config /etc/security/limits.conf "${LIMITS_CONF[@]}"
  replace_config /etc/pam.d/login "${LOGIN_CONF[@]}"
  sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
  setenforce 0
}

# 第一个错误的处理
err1(){
  sudo yum install -y glibc-static
  err_file="$ORACLE_HOME/ctx/lib/ins_ctx.mk"
  [ -f "$err_file" ] || err_quit 1 "没有找到文件 $err_file"
  sed -e 's+.*$(LINK_CTXHX)\s*$(CTXHXOBJ)\s*$(INSO_LINK).*+\t-static $(LINK_CTXHX) $(CTXHXOBJ) $(INSO_LINK) /usr/lib64/stdc.a+' -i "$err_file"
}

# 第二个错误的处理
err2(){
  err_file="$ORACLE_HOME/sysman/lib/ins_emagent.mk"
  [ -f "$err_file" ] || err_quit 1 "没有找到文件 $err_file"
  sed -e 's/\s*$(MK_EMAGENT_NMECTL).*/\t$(MK_EMAGENT_NMECTL) -lnnz11/' -i "$err_file"
}

# 安装成服务
service_install(){
cat << EOF >/etc/init.d/oracle
#!/usr/bin/env bash

# chkconfig: 345 90 10
# description: oracle 数据库
#

. /etc/rc.d/init.d/functions

ORACLE_HOME="$ORACLE_HOME"
ORACLE_USER="$USER"
LSNRCTL="\$ORACLE_HOME/bin/lsnrctl"
DBSTART="\$ORACLE_HOME/bin/dbstart"
DBSHUT="\$ORACLE_HOME/bin/dbshut"

case "\$1" in
  start)
    if \$LSNRCTL status 2>&1 >/dev/null; then
      echo "ORACLE 正在运行中"
      exit 1
    fi
    su - \$ORACLE_USER -c "
      \$LSNRCTL start
      \$DBSTART \$ORACLE_HOME
    "
  ;;
  stop)
    su - \$ORACLE_USER -c "
      \$LSNRCTL stop
      \$DBSHUT \$ORACLE_HOME
    "
  ;;
  restart)
    \$0 stop
    \$0 start
  ;;
  status)
    \$LSNRCTL status
    exit \$?
  ;;
  *)
    echo "Usage: \$0 [start|stop|restart|status]"
    exit 1
  ;;
esac
EOF
  chmod +x /etc/init.d/oracle
  sed "s+\s*$ORACLE_SID.*+$ORACLE_SID:$ORACLE_HOME:Y+" -i /etc/oratab
  systemctl daemon-reload
  service oracle restart
  chkconfig oracle on
}

# 安装vnc
vnc_install(){
  curl -sSL $HOST/vnc/install.sh|bash -s -- install --user=$USER --password=$PASSWORD install-font --oracle
}

# 是否包含 localhost
has_localhost(){
  for el in $@; do
    if [ "$el" == "localhost" ]; then
      return 0
    fi
  done
  return 1
}

# /etc/hosts 文件里的 localhost 检查
# 若没有配置 1521 端口 无法启动
hostname_check(){
  v4_str="$(grep '^127.0.0.1' /etc/hosts)"
  v4_arr=($(echo ${v4_str}))
  if ! has_localhost "${v4_arr[@]}"; then
    echo "/etc/hosts 127.0.0.1 行没有 localhost 添加"
    sed "s/^127\.0\.0\.1.*/$(echo "${v4_str}") localhost/" -i /etc/hosts
  fi
  v6_str="$(grep '^::1' /etc/hosts)"
  v6_arr=($(echo ${v6_str}))
  if ! has_localhost "${v6_arr[@]}"; then
    echo "/etc/hosts ::1 行没有 localhost 添加"
    sed "s/^::1.*/$(echo "${v6_str}") localhost/" -i /etc/hosts
  fi
}

# 配置环境变量
env_config(){
  line="$(grep -in '\s*export.*PATH' /etc/profile|awk -F: '{print $1}'|sort -r|head -n 1)"
  [ -z "$line" ] && err_quit 1 "获取配置错误"
  path="export ORACLE_HOME=$ORACLE_HOME\nexport ORACLE_SID=$ORACLE_SID\nexport NLS_LANG=AMERICAN_AMERICA.AL32UTF8\nPATH=\$ORACLE_HOME/bin:\$PATH"
  sed "${line}i$path" -i /etc/profile
}

for pm in "$@"; do
  case "$pm" in
    step1)
      step1; shift;;
    err1)
      err1; shift;;
    err2)
      err2; shift;;
    vnc)
      vnc_install; shift;;
    env)
      env_config
      hostname_check
      shift;;
    svs)
      service_install; shift;;
    *)
      if [[ "$pm" =~ ^-- ]]; then
        shift
      else
        echo "未知命令 $pm"
        echo "查看更多命令请运行: $0 --help"
      fi
      ;;
  esac
done
